package com.wffz.controller;

import com.wffz.po.*;
import com.wffz.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

@Controller
public class MainController {
    private String prefix = "main";


    @Resource
    private UserService userService;

    @Resource
    private MoneyLogService moneyLogService;

    @Resource
    private NoticeService noticeService;

    @Resource
    private PatchService patchService;

    @Resource
    private ShareRecordService recordService;

    @Resource
    private WffzConfigService configService;

    @Resource
    private WithDrawRecordService withDrawRecordService;

    @Resource
    private UserCollectBankService userCollectBankService;

    @Resource
    private IncomeLogService incomeLogService;

    @RequestMapping("/mime")
    public String mime(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());

        clientUser.setBalance(clientUser.getBalance() == null ? 0 : clientUser.getBalance());
        clientUser.setAwayBalance(clientUser.getAwayBalance() == null ? 0 : clientUser.getAwayBalance());
        String key = "follow_us_url";
        WffzConfig config = configService.getConfigByKey(key);
        String follow_us_url = "";
        if (config != null) {
            follow_us_url = config.getConfigValue();
        }
        model.addAttribute("user", clientUser);
        model.addAttribute("follow_us_url", follow_us_url);
        return prefix + "/mime";
    }

    /**
     * @Description: [充值页面]
     * @Title: recharge
     * @Author: SUNLEI
     * @Date: 2023-05-10
     * @Param: request
     * @Param: model
     * @Return: java.lang.String
     * @Throws:
     */
    @RequestMapping("/recharge")
    public String recharge(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        clientUser.setBalance(clientUser.getBalance() == null ? 0 : clientUser.getBalance());
        clientUser.setAwayBalance(clientUser.getAwayBalance() == null ? 0 : clientUser.getAwayBalance());
        model.addAttribute("user", clientUser);
        return prefix + "/recharge";
    }


    @RequestMapping("/invite")
    public String promotion(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        float withDrawAmount = userService.getWithDrawAmount(clientUser);
        model.addAttribute("user", clientUser);
        //
        float income = incomeLogService.queryAmountByTypes(new String[]{"3","4"},clientUser.getLoginName(),true);
        float dayIncome = incomeLogService.queryAmountByTypes(new String[]{"3","4"},clientUser.getLoginName(),true);
        // 今日邀请人数
        List<ShareRecord> list = recordService.findMyShareRecord();
        // 今日邀请人数
        List<ShareRecord> nowDayList = recordService.findMyTodayShareRecord();
        // 今日邀请注册的返佣
        float amount = incomeLogService.queryAmount("4", clientUser.getLoginName(), false);
        float dayAmount = incomeLogService.queryAmount("4", clientUser.getLoginName(), true);
        // 下注返佣
        float CommissionsBonus  = incomeLogService.queryAmount("3", clientUser.getLoginName(), false);
        model.addAttribute("shareCount", list.size());
        model.addAttribute("shareDayCount", nowDayList.size());
        model.addAttribute("amount", amount);
        model.addAttribute("dayAmount", dayAmount);
        model.addAttribute("incomeAmount", income);
        model.addAttribute("dayIncomeAmount", dayIncome);
        model.addAttribute("withDrawAmount", withDrawAmount);
        model.addAttribute("CommissionsBonus", CommissionsBonus);
        return prefix + "/invite";
    }


    @RequestMapping("/reward")
    public String reward(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        // 获取提现记录
        int shareCount = userService.shareCount(clientUser);
        // 获取充值记录
        int moneyCount = moneyLogService.moneyCount(clientUser);
        boolean firstRecharge = false;
        boolean secondRecharge = false;
        boolean firstShareCount = false;
        if (moneyCount >= 1) {
            firstRecharge = true;
        }
        if (moneyCount >= 2) {
            secondRecharge = true;
        }
        if (shareCount >= 1) {
            firstShareCount = true;
        }
        model.addAttribute("firstRecharge", firstRecharge);
        model.addAttribute("secondRecharge", secondRecharge);
        model.addAttribute("firstShareCount", firstShareCount);
        return prefix + "/reward";
    }


    @RequestMapping("/win")
    public String win(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        clientUser.setBalance(clientUser.getBalance() == null ? 0 : clientUser.getBalance());
        clientUser.setAwayBalance(clientUser.getAwayBalance() == null ? 0 : clientUser.getAwayBalance());
        //
        List<Notice> list = noticeService.queryNoticeByType("2");
        List<Notice> list2 = noticeService.queryNoticeByType("1");
        Notice notice = null;
        if (list != null && list.size() > 0) {
            notice = list.get(0);
        }
        Notice alertNotice = null;
        if (list2 != null && list2.size() > 0) {
            alertNotice = list2.get(0);
        }
        model.addAttribute("user", clientUser);
        model.addAttribute("notice", notice);
        model.addAttribute("alertNotice", alertNotice);
        return prefix + "/win";
    }

    @RequestMapping("/red_win")
    public String redWin(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        model.addAttribute("user", clientUser);
        return prefix + "/red_win";
    }

    @RequestMapping("/record")
    public String record(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        model.addAttribute("user", clientUser);
        return prefix + "/recharge_record";
    }

    @RequestMapping("/help")
    public String help() {
        return prefix + "/help";
    }

    @RequestMapping("/landing")
    public String landing() {
        return prefix + "/landing";
    }


    @RequestMapping("/privacypolicy")
    public String privacypolicy() {
        return prefix + "/privacypolicy";
    }

    @RequestMapping("/agreement")
    public String agreement() {
        return prefix + "/agreement";
    }


    @RequestMapping("/winHelp")
    public String winHelp() {
        return prefix + "/win_help";
    }

    @RequestMapping("/withdrawal")
    public String withdrawal(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        // 计算可提现金额
        // 可提现金额=对应会员可提现比例*充值金额-已提现金额
        // 充值金额
        Float money = moneyLogService.sumMoney(clientUser);
        Float withdraw = withDrawRecordService.withDrawMoney(clientUser);
        int vipLevel = clientUser.getVipLevel() == null ? 0 : clientUser.getVipLevel();
        Float txje = money * vipLevel - withdraw;
        clientUser.setWithdrawAmount(txje);
        userService.updateClientUser(clientUser);
        model.addAttribute("user", clientUser);
        return prefix + "/with_drawal";
    }

    @RequestMapping("/addBank")
    public String addBank() {
        return prefix + "/add_bank";
    }

    @RequestMapping("/bankCard")
    public String bankCard(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        List<UserCollectBank> list = userCollectBankService.getBankList(clientUser);
        model.addAttribute("bankList", list);
        return prefix + "/bank_card";
    }

    @RequestMapping("/answer")
    public String answer() {
        return prefix + "/answer";
    }


    @RequestMapping("/redGreenRecords")
    public String redGreenRecords(HttpServletRequest request, Model model) {
        String patchType = request.getParameter("patchType");
        List<Patch> list = patchService.queryAwardPatchAll(patchType);
        model.addAttribute("patchList", list);
        return prefix + "/redGreenRecords";
    }

    @RequestMapping("/withDrawRecord")
    public String withDrawRecord(HttpServletRequest request, Model model) {

        return prefix + "/withdraw_record";
    }

    @RequestMapping("/income")
    public String income(HttpServletRequest request, Model model) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        return prefix + "/income";
    }
}
